package com.mh.mh.netty.socket;

import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class SocketServer {
	private static SocketServer socketServer = new SocketServer();
	private Socket socket;
	private Thread sThread;
	private InputStream inputStream;
	private DataInputStream input;
	private OutputStream os;
	private boolean isConnect = false;

	public boolean getIsConnect() {
		return isConnect;
	}

	public void setIsConnect(boolean isConnect) {
		this.isConnect = isConnect;
	}

	public Thread getsThread() {
		return sThread;
	}

	public void setsThread(Thread sThread) {
		this.sThread = sThread;
	}

	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}

	/**
	 * 单例模式
	 *
	 * @return
	 */
	synchronized public static SocketServer getInstance() {
		return socketServer;
	}

	public byte[] getBackCode(int i) {
		byte[] b = new byte[i];
		try {
			inputStream = socket.getInputStream();
			input = new DataInputStream(inputStream);
			int readCount = 0; // 已经成功读取的字节的个数 
			while (readCount < i) {
			    readCount += input.read(b, readCount, i - readCount);    
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}
	
	public byte[] StartListen(int i) {
		byte[] b = new byte[i];
		try {
			inputStream = socket.getInputStream();
			input = new DataInputStream(inputStream);
			input.read(b);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}
	
//	public void StartListen() {
//		sThread = new Thread(new Runnable() {
//			public void run() {
//				try {
//					inputStream = socket.getInputStream();
//					input = new DataInputStream(inputStream);
//					byte[] b = new byte[12];
//					int length = 0;
//					if ((length = input.read(b)) > 0) {
//						EventBus.getDefault().post(
//								new DataCode(12, b));
//					}
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//		sThread.start();
//	}

	public void StopListen() {
		try {
			input.close();
			inputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送字节
	 */
	public void sendCode(byte[] Code) {
		try {
			os = socket.getOutputStream();
			os.write(Code);
			os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 判断是否断开连接，断开返回true,没有返回false
	 *
	 * @return
	 */
	public Boolean isServerClose() {
		try {
			byte[] head = new byte[12];
			int Cmd = 666;
			int ContentSize = 0;
			float UTCtime = System.currentTimeMillis();
			System.arraycopy(DataUtils.intToBytes(Cmd), 0, head, 0, 4);
			System.arraycopy(DataUtils.intToBytes(ContentSize), 0, head, 4, 4);
			System.arraycopy(DataUtils.float2byte(UTCtime), 0, head, 8, 4);
			os = socket.getOutputStream();
			os.write(head);
			os.flush();
			return false;
		} catch (Exception e) {
			return true;
		}

	}

	/**
	 * 关闭socket连接
	 */
	public void closeSocket() {
		if (socket != null) {
			try {
				socket.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
